package com.thornbird.vehiclerecycling.service.impl;

import com.thornbird.vehiclerecycling.entity.User;
import com.thornbird.vehiclerecycling.exception.NotFoundException;
import com.thornbird.vehiclerecycling.exception.ValidationException;
import com.thornbird.vehiclerecycling.model.record.PageList;
import com.thornbird.vehiclerecycling.model.record.UserRecord;
import com.thornbird.vehiclerecycling.repository.UserRepository;
import com.thornbird.vehiclerecycling.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    private final UserRepository userRepository;

    @Autowired
    public UserServiceImpl(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public Boolean isActive(UserRecord userRecord) {
        Boolean active = false;
        if (userRecord != null) {
            User user = userRepository.findFirstById(userRecord.getId());
            if (user != null) {
                active = user.getActive();
                active = active == null ? false : active;
            }
        }
        return active;
    }

    @Override
    public Boolean isSuperUser(UserRecord userRecord) {
        Boolean superUser = false;
        if (userRecord != null) {
            User user = userRepository.findFirstById(userRecord.getId());
            if (user != null) {
                superUser = user.getSuperUser();
                superUser = superUser == null ? false : superUser;
            }
        }
        return superUser;
    }

    @Override
    public PageList<UserRecord> findUsers(String keyword, Integer pageNumber, Integer pageSize) {
        Pageable pageable = PageList.generatePageable(pageNumber, pageSize);
        keyword = keyword == null ? "" : keyword.toLowerCase();
        keyword = "%" + keyword + "%";
        Page<User> page = userRepository.findByCondition(keyword, pageable);
        return PageList.convertPage(page, UserRecord.class);
    }

    @Override
    public UserRecord saveUser(User user) throws NotFoundException, ValidationException {
        User currentUser = userRepository.findFirstById(user.getId());
        if (currentUser == null) {
            throw new NotFoundException();
        }

        if (user.getSuperUser() == null) {
            user.setSuperUser(false);
        }
        if (user.getActive() == null) {
            user.setActive(false);
        }
        if (currentUser.getSuperUser() && (!user.getSuperUser() || !user.getActive())) {
            throw new ValidationException("超级用户不允许修改“超级用户”及“启用”字段");
        }

        if (!currentUser.getSuperUser() && user.getSuperUser()) {
            throw new ValidationException("非超级用户不允许修改为超级用户");
        }

        currentUser.setActive(user.getActive());
        currentUser.setRealName(user.getRealName());
        currentUser.setCompanyName(user.getCompanyName());
        userRepository.save(currentUser);
        UserRecord userRecord = new UserRecord();
        userRecord.parseEntity(userRepository.findFirstById(currentUser.getId()));
        return userRecord;
    }

}
